Scroll down until you find the table under the “All Installers” section
Find the row for “macOS 10.15+”
Click “RStudio-2022.XX.X-XXX.dmg”
Run the downloaded .dmg file
Drag the RStudio icon to your Applications folder (if you want)
RStudio Window
R will Grant your Wishes
R is like a well-meaning but overly literal genie
It has the power to grant almost any wish
But we must phrase our wishes carefully!
We will always get what we ask for…
…but not always what we wanted.
Mastering the R language means learning…
How to properly phrase commands
How to decipher error messages
How to view code from R’s perspective
How to detect and correct small mistakes
Console Live Coding
# Addition and Subtraction10+310-310+3# spaces are optional but recommended10-3# ==============================================================================# Multiplication and Division10*3# correct10 x 3# error10/3# correct10 \ 3# error# ==============================================================================# Parentheses and Exponents10+3*2(10+3) *210^210^ (1/2)# ==============================================================================# Negative and Large Numbers10+-309876543# correct9,876,543# error9876543# error
Assignment
It is often useful to store data in named objects
This makes the data easier to use and re-use
This makes the code easier to write and read
Which command is easier to follow?
Dial 7 8 5 8 6 4 0 8 4 1
Call Office Phone
Named objects are created using assignment
Give a name then an arrow then the data
office <- 7858640841
Assignment Live Coding
# LESSON: Assigning and printingx <-2x# ==============================================================================# USECASE: Using an object in math (a la algebra) x *42*4# ==============================================================================# LESSON: You must use assignment to update an objectxx +1x # still 2x <- x +1x # updated to 3# ==============================================================================# USECASE: We can use the same object multiple times in a line(10+ x -1) / x# ==============================================================================# USECASE: We can also use an object to create another objecty <-10+ xy# ==============================================================================# USECASE: We can also use multiple objects in a liney / x
Naming
Object names can only include:
Letters: a-Z
Numbers: 0-9
Underscores: _
Periods: .
Additional Rules:
Must start with a letter or period
Cannot contain spaces or dashes
Cannot contain other symbols
Names are case-sensitive (age ≠ Age)
Naming Live Coding
# LESSON: Good names are a balancing actx <-93# what is it?rate <-93# too shortheart_rate_in_beats_per_minute <-93# too longheart_rate_bpm <-93# just right# ==============================================================================# PITFALL: Don't try to include spaces or dashes in namesheart rate <-93# errorheart-rate <-93# error# ==============================================================================# PITFALL: Don't try to include special symbolsage@time2 <-12# errorage_time2 <-12# correct# ==============================================================================# PITFALL: Don't try to put a number or underscore firstheart_rate_1 <-93# correct1_heart_rate <-93# error_heart_rate <-93# error# ==============================================================================# LESSON: Object names are case-sensitiveheart_rate <-93Heart_rate <-88heart_rate # still 93Heart_rate # a new object
Functions
Recipes allow chefs to cook up tasty treats
Recipes call for ingredients
Recipes involve one or more steps
Steps transform ingredients into treats
Functions are like customizable recipes
Functions call for inputs (“arguments”)
Functions involve one or more lines of code
Code transforms inputs into outputs
Using functions requires parentheses (usually)
out <- f(in1, in2)
Functions Live Coding
# USECASE: Function can perform a task more easily and readably# TEMPLATE: output <- function_name(input)9^ (1/2)x <-sqrt(9)x# ==============================================================================# LESSON: We can also use functions to transform objectsy <-9sqrt(y)# ==============================================================================# LESSON: We can even use functions to transform the result of calculations2/3round(2/3)# ==============================================================================# LESSON: We can customize what a function does using arguments# TEMPLATE: output <- function_name(argument, argument_name = argument_value)round(2/3, digits =2)round(2/3, digits =3)# ==============================================================================# LESSON: Some arguments are optional because they have default valuesround(2/3) # the default value for digits is 0round(2/3, digits =0)
Strings
When talking to R, we need a way to distinguish
Object/function names (e.g., the mean function)
Text/character data (e.g., the word mean)
Strings are R’s way of storing text data
Strings can store any characters (no rules!)
Strings are created and displayed with quotes
R has great tools for working with strings
Strings can be collected into vectors
Special functions can transform strings
name <- "John Doe"
Strings Live Coding
# USECASE: Strings are the main way to store character data in Rmy_color <- red # errormy_color <-"red"# correct# ==============================================================================# USECASE: Strings can also store symbols not allowed in object namesdye <-"red#40"dyedyes <-c("red#40", "blue#02")dyes# ==============================================================================# PITFALL: Many operations you can do to numbers won't work for stringsdyes +1# errormean(dyes) # error# ==============================================================================# USECASE: But other operations work for both or even just for stringslength(dyes)nchar(dyes)dyes2 <-toupper(dyes)dyes2
Packages
Cookbooks are a great way to learn to cook
They contain lots of recipes and instructions
Browse an online bookstore for a cookbook
Order it to add it to your personal bookshelf
To use, pull the cookbook off the shelf
Packages are like cookbooks for R
They contain helpful functions and datasets
Browse an online repository for a package
Install it to add it to your personal library
To use, load the package from the library
library("pkg_name")
Packages Live Coding
# USECASE: The stringr package adds a function to fix capitalizationstudents <-c("mary anne", "BENjamin", "Lee")# ==============================================================================# PITFALL: But we can't use that function without installing the packagestr_to_title(students) # error# ==============================================================================# LESSON: Installing a package using RStudio# - RStudio > Extras pane > Packages tab > Install button# ==============================================================================# PITFALL: We also need to load the package before we can use itstr_to_title(students) # error# ==============================================================================# LESSON: We load the package using library()library("stringr")str_to_title(students) #finally works!# ==============================================================================# LESSON: We can also keep our packages updated using RStudio# RStudio > Extras pane > Packages tab > Update button
Importing and Exporting
Data is usually stored in data files
Importing files into R is called reading
Exporting files from R is called writing
A convenient data file type is a CSV
This stands for comma-separated values
A CSV file is easy to share with other people
The tidyverse package can read/write CSVs
Other packages can read/write other types (e.g., readxl, haven, rio, googlesheets4)
Read/Write Live Coding
# SETUP: Load the tidyverse package (if you haven't yet)library(tidyverse)# ==============================================================================# USECASE: Read in a file containing datamy_data <-read_csv("jz2017.csv")my_data# NOTE: read_csv() will examine and guess the data type of each variable.# You can tell it the data type of each variable, but that is more advanced.# ==============================================================================# USECASE: Write data back to a filewrite_csv(my_data, file ="jz2017b.csv")# NOTE: You can see the new file in Extras pane > Files tab.# You can open the file in another program (e.g., Microsoft Excel).# You can also email this file to someone else to share it.